(() => {

'use strict';

goog.provide('Blockly.Blocks');

goog.require('path');

const mediaDirPath = path.join(document.currentScript.src, '../../media/');

// Blockly.Blocks.RedSugar.HUE = "#FF6680";
Blockly.Msg['RedSugar_onelineultrasonic'] = '#eb984e';
Blockly.Msg['RedSugar_MP3'] = '#59abe9';
Blockly.Msg['RedSugar_motor'] = '#66cccc';
Blockly.Msg['RedSugar_KEY'] = '#ffcc66';

var dual_motor_MS=[["MA", "1"],["MB", "2"]];

/*******三线超声波*******/
Blockly.Blocks.onelineultrasonic = {
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_onelineultrasonic']);
    this.appendDummyInput("")
        .appendField('三线超声波测距(cm)');
	  this.appendValueInput("PIN", Number)
        .appendField('管脚#')
        .setCheck(Number);
	  this.setInputsInline(true);
    this.setOutput(true, Number);
    this.setTooltip("");
  }
};

/*******串口MP3*******/
Blockly.Blocks.MP3_init={
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_MP3']);
    this.appendDummyInput("")
        .appendField(new Blockly.FieldImage(mediaDirPath + "RedSugar/module/MP3.png", 70, 32))
        .appendField('串口MP3 软串口 ');
    this.appendValueInput("PIN1")
        .setCheck(Number)
        .setAlign(Blockly.ALIGN_RIGHT)
        .appendField("Rx");
    this.appendValueInput("PIN2")
        .setCheck(Number)
        .setAlign(Blockly.ALIGN_RIGHT)
        .appendField("Tx");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setTooltip('');    
  }
};

Blockly.Blocks.MP3_STATE = {
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_MP3']);
    this.appendDummyInput("")
        .appendField(new Blockly.FieldImage(mediaDirPath + "RedSugar/module/MP3.png", 70, 32))
        .appendField('串口MP3');    
	  this.appendDummyInput("")
        .appendField('播放控制')
        .appendField(new Blockly.FieldDropdown([['播放', "play()"],['暂停', "pause()"], ['停止', "stop()"],['下一曲', "next_song()"],['上一曲', "last_song()"],['音量加', "vol_up()"],['音量减', "vol_dn()"]]), "STAT");
    this.setInputsInline(true);
	  this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};

Blockly.Blocks.MP3_MODE = {
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_MP3']);
    this.appendDummyInput("")
        .appendField(new Blockly.FieldImage(mediaDirPath + "RedSugar/module/MP3.png", 70, 32))
        .appendField('串口MP3'); 
	  this.appendDummyInput("")
        .appendField('播放模式')
        .appendField(new Blockly.FieldDropdown([['全部循环', "loop_play()"],['单曲循环', "loop_single_play()"], ['单曲停止', "single_stop()"], ['随机播放', "random_play()"],['顺序播放', "order_play()"]]), "STAT");
    this.setInputsInline(true);
	  this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
  }
};

Blockly.Blocks.MP3_PLAY={
  init:function(){
    this.setColour(Blockly.Msg['RedSugar_MP3']);
    this.appendDummyInput("")
        .appendField(new Blockly.FieldImage(mediaDirPath + "RedSugar/module/MP3.png", 70, 32))
        .appendField('串口MP3'); 
	  this.appendValueInput('NUM',Number)
      .setCheck(Number)
      .appendField('播放曲目');
    this.appendDummyInput("")
      .appendField('音量(0~30)%')
      .appendField(new Blockly.FieldNumber(20, 1, 30, 1), "VOLUME");
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  }
};

/*******电机*******/
Blockly.Blocks.dual_motor={
init:function(){
    this.setColour(Blockly.Msg['RedSugar_motor']);
    this.appendDummyInput("")
      .appendField('双路电机')
	    .appendField(new Blockly.FieldDropdown(dual_motor_MS), "PORT");
    this.appendValueInput('speed')
        .setCheck(Number)
        .appendField('输出(-255~255)');
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  }
};

Blockly.Blocks.dual_motor_2={
  init:function(){
      this.setColour(Blockly.Msg['RedSugar_motor']);
      this.appendDummyInput("")
        .appendField('UNO扩展双路电机')
      this.appendValueInput('speed_MC')
          .appendField('MC')
          .setCheck(Number);
      this.appendValueInput('speed_MD')
          .appendField('MD')
          .setCheck(Number);
      this.appendDummyInput("")
          .appendField('输出(-255~255)');
      this.setInputsInline(true);
      this.setPreviousStatement(true);
      this.setNextStatement(true);
    }
  };

var KEY_=[["0", "K0"],["1", "K1"],["2", "K2"],["3", "K3"],["4", "K4"],["5", "K5"],["6", "K6"],["7", "K7"],["8", "K8"],["9", "K9"]];

  /*******矩阵按键*******/
Blockly.Blocks.KEY_password_judgment={
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_KEY']);
    this.appendDummyInput("")
        .appendField('I2C矩阵按键 开启密码 ');
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setTooltip('');    
  }
};

Blockly.Blocks.KEY_password_clear={
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_KEY']);
    this.appendDummyInput("")
        .appendField('I2C矩阵按键 清除密码缓存 ');
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setTooltip('');    
  }
};

Blockly.Blocks.KEY_password={
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_KEY']);
    this.appendDummyInput("")
        .appendField('I2C矩阵按键 设置密码 ')
        .appendField(new Blockly.FieldDropdown(KEY_), "KEY_1")
        .appendField(new Blockly.FieldDropdown(KEY_), "KEY_2")
        .appendField(new Blockly.FieldDropdown(KEY_), "KEY_3");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setTooltip('');    
  }
};

var KEY_Read=[["密码判断", "3"],["第一次", "0"],["第二次", "1"],["第三次", "2"]];

Blockly.Blocks.KEY_password_Read = {
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_KEY']);
    this.appendDummyInput("")
        .appendField('I2C矩阵按键 ')
        .appendField(new Blockly.FieldDropdown(KEY_Read), "KEY_read");
	  this.setInputsInline(true);
    this.setOutput(true, Number);
    this.setTooltip("");
  }
};

Blockly.Blocks.KEY_Read = {
  init: function() {
    this.setColour(Blockly.Msg['RedSugar_KEY']);
    this.appendDummyInput("")
        .appendField('I2C矩阵按键 读按键值')
	  this.setInputsInline(true);
    this.setOutput(true, Number);
    this.setTooltip("");
  }
};

// Blockly.Blocks.dual_motor={
// init:function(){
//     this.setColour(Blockly.Blocks.RedSugar.HUE);
//     this.appendDummyInput("")
// 		.appendField('双路电机')
// 		.appendField("#")
// 	    .appendField(new Blockly.FieldDropdown(dual_motor_MS), "PORT");
//     this.appendValueInput('speed')
//         .setCheck(Number)
//         .appendField('转速(-100~100)%');
//     this.setInputsInline(true);
//     this.setPreviousStatement(true);
//     this.setNextStatement(true);
//   }
// };

// Blockly.Blocks.MP3_VOL={
// init:function(){
//     this.setColour(Blockly.Blocks.RedSugar.HUE);
//     this.appendDummyInput("")
//       .appendField('MP3 RX管脚#10')
// 	this.appendValueInput("TX_PIN", Number)
//         .appendField('TX管脚#')
//         .setCheck(Number);
// 	this.appendValueInput('VOLUME',Number)
//       .setCheck(Number)
//       .appendField('音量(0~30)%');
//     this.setInputsInline(true);
//     this.setPreviousStatement(true);
//     this.setNextStatement(true);
//   }
// };

// Blockly.Blocks.MP3_PLAY={
// init:function(){
//     this.setColour(Blockly.Blocks.RedSugar.HUE);
//     this.appendDummyInput("")
//       .appendField('MP3 RX管脚#10')
// 	this.appendValueInput("TX_PIN", Number)
//         .appendField('TX管脚#')
//         .setCheck(Number);
// 	this.appendValueInput('NUM',Number)
//       .setCheck(Number)
//       .appendField('播放曲目');
//     this.setInputsInline(true);
//     this.setPreviousStatement(true);
//     this.setNextStatement(true);
//   }
// };

// Blockly.Blocks.MP3_STATE = {
//   init: function() {
//     this.setColour(Blockly.Blocks.RedSugar.HUE);
//     this.appendDummyInput("")
//         .appendField('MP3 RX管脚#10')
// 	this.appendValueInput("TX_PIN", Number)
//         .appendField('TX管脚#')
//         .setCheck(Number);
// 	this.appendDummyInput("")
//         .appendField('播放控制')
//         .appendField(new Blockly.FieldDropdown([['播放', "play()"],['暂停', "pause()"], ['停止', "stop()"],['下一曲', "next_song()"],['上一曲', "last_song()"],['音量加', "vol_up()"],['音量减', "vol_dn()"]]), "STAT");
//     this.setInputsInline(true);
// 	this.setPreviousStatement(true, null);
//     this.setNextStatement(true, null);
//   }
// };

// Blockly.Blocks.MP3_MODE = {
//   init: function() {
//     this.setColour(Blockly.Blocks.RedSugar.HUE);
//     this.appendDummyInput("")
//         .appendField('MP3 RX管脚#10')
// 	this.appendValueInput("TX_PIN", Number)
//         .appendField('TX管脚#')
//         .setCheck(Number);
// 	this.appendDummyInput("")
//         .appendField('播放模式')
//         .appendField(new Blockly.FieldDropdown([['全部循环', "loop_play()"],['单曲循环', "loop_single_play()"], ['单曲停止', "single_stop()"], ['随机播放', "random_paly()"],['顺序播放', "order_play()"]]), "STAT");
//     this.setInputsInline(true);
// 	this.setPreviousStatement(true, null);
//     this.setNextStatement(true, null);
//   }
// };

// Blockly.Blocks.MP3_PLAY_DISTANCE={
// init:function(){
//     this.setColour(Blockly.Blocks.RedSugar.HUE);
//     this.appendDummyInput("")
//       .appendField('MP3 RX管脚#10')
// 	this.appendValueInput("TX_PIN", Number)
//         .appendField('TX管脚#')
//         .setCheck(Number);
// 	this.appendValueInput('NUM',Number)
//       .setCheck(Number)
//       .appendField('播放距离(cm)');
//     this.setInputsInline(true);
//     this.setPreviousStatement(true);
//     this.setNextStatement(true);
//   }
// };

})();




